/* ----------------------------------------------------------------------------

	pax.plugin.widget.googlemaps.js Copyright (C) 2008 Mikkel Bergmann, Pointful

	Licence
	-------
	
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

	See lgpl.txt for licence details

---------------------------------------------------------------------------- */

/*
	Script: pax.plugin.widget.googlemaps
		This is a google maps widget plugin for PAX
		
	Author:
		Mikkel Bergmann, <http://www.pointful.com>

	License:
		GNU General Public License.

	Comments:

*/

var pax = pax || {};
pax.plugin = pax.plugin || {};
pax.plugin.widget = pax.plugin.widget || {};
pax.plugin.widget.googlemaps = pax.plugin.widget.googlemaps || {};
pax.plugin.widget.googlemaps.geocoder = null;
pax.plugin.widget.googlemaps.elements = {};										//	element holder for assigned google maps object, eg: panorama or map obj.

pax.plugin.widget.googlemaps.initMap = function( element, args ) {
	element = pax.$( element );		//	Ensure we have a DOM element
	args = args || {};
	args.address = args.address || '';
	if( typeof( args.showControls ) == 'undefined' )args.showControls = true;	//	Show or hide all controls
	args.controlConfig = args.controlConfig || {								//	Individual control config
		type: true,
		zoom: true
	};
	if( typeof( args.showMarker ) == 'undefined' )args.showMarker = true;		//	Show a marker, on load of map

	//	Note: GBrowserIsCompatible will be undefined, if we haven't loaded the g.maps script
	//	Perhaps use try ... catch?
	
	var gbic = false;
	try {
		gbic = GBrowserIsCompatible();
	} catch  ( e ) {
		//	Can't continue... Need a handler hook for this...
		if( args.errorHandler ) {
			args.errorHandler( 1 );
		} else {
			element.innerHTML = 'Error: could not start Google Maps';
		}
		return false;
	};
	
	if( gbic ) {
		var map = new GMap2( element );
		
		if( args.showControls ) {
			if( args.controlConfig.type )map.addControl( new GMapTypeControl() );
			if( args.controlConfig.zoom )map.addControl( new GLargeMapControl() );
		}
		
		if( ! pax.plugin.widget.googlemaps.geocoder )pax.plugin.widget.googlemaps.geocoder = new GClientGeocoder();
		var geocoder = pax.plugin.widget.googlemaps.geocoder;
		
		//	Use the JSON response to interface with gmaps
		geocoder.getLocations( args.address, function( response ) {
			map.clearOverlays();
			if( ! response || response.Status.code != 200 ) {
				if( args.errorHandler ) {
					args.errorHandler( response );
				} else {
					//	TODO: Handle diffrently!
					alert("Sorry, we were unable to geocode that address: [" + args.address + "]");
				}
			} else {
				place = response.Placemark[0];
				point = new GLatLng( place.Point.coordinates[1], place.Point.coordinates[0] );
				map.setCenter( point, 14 );
				if( args.showMarker ) {
					marker = new GMarker( point );
					map.addOverlay( marker );
					marker.openInfoWindowHtml( place.address + '<br><b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode );
				}
				pax.plugin.widget.googlemaps.elements[element] = map;
			}
		} );

		
	}
	
};


pax.plugin.widget.googlemaps.initStreetview = function( element, args ) {
	element = pax.$( element );		//	Ensure we have a DOM element
	args = args || {};
	args.address = args.address || '';
	if( typeof( args.showControls ) == 'undefined' )args.showControls = true;	//	Show or hide all controls
	args.controlConfig = args.controlConfig || {								//	Individual control config
		type: true,
		zoom: true
	};
	if( typeof( args.showMarker ) == 'undefined' )args.showMarker = true;		//	Show a marker, on load of map

	var gbic = false;
	try {
		gbic = GBrowserIsCompatible();
	} catch  ( e ) {
		//	Can't continue...
		if( args.errorHandler ) {
			args.errorHandler( 1 );
		} else {
			element.innerHTML = 'Error: could not start Google Street view';
		}
		return false;
	};
	
	if( gbic ) {
		if( ! pax.plugin.widget.googlemaps.geocoder )pax.plugin.widget.googlemaps.geocoder = new GClientGeocoder();
		var geocoder = pax.plugin.widget.googlemaps.geocoder;
		
		//	Use the JSON response to interface with gmaps
		geocoder.getLocations( args.address, function( response ) {
			if( ! response || response.Status.code != 200 ) {
				//	TODO: Handle diffrently!
				alert("Sorry, we were unable to geocode that address");
			} else {
				place = response.Placemark[0];
				point = new GLatLng( place.Point.coordinates[1], place.Point.coordinates[0] );
				
				var myPano = new GStreetviewPanorama( element, { 
					latlng: point	//,	pov: { yaw: 370.64659986187695, pitch: -20 }
				} );
				
				GEvent.addListener( myPano, "error", function( errorCode ) {
					if( args.errorHandler ) {
						args.errorHandler( errorCode );
					} else {
						if( errorCode == 600 ) {
							alert( "Error: no streetview panorama available for this location." );
							return;
						} else if( errorCode == 603 ) {
							alert("Error: Flash doesn't appear to be supported by your browser");
							return;
						}
					}
				} );
				
				pax.plugin.widget.googlemaps.elements[element] = myPano;
			}
		} );
		
	}
};

//	Returns the associated object for a given element
pax.plugin.widget.googlemaps.getObject = function( element ) {
	return pax.plugin.widget.googlemaps.elements[element];
};
